package tank.server.game;

import java.awt.*;
import java.util.Vector;

import static tank.server.game.MyGame.*;

/**
 * 坦克
 */
public class Tank {

    // x坐标
    private int x;
    // y坐标
    private int y;
    // 颜色
    private Color color;
    // 方向
    private int direction;
    // 速度
    private int speed = 2;
    // 子弹集合
    private Vector<Bullet> bullets = new Vector<>();
    // 生命值
    private volatile int live = 1;
    // 是否移除界面
    private volatile boolean removed = false;
    // 是否启动
    private volatile boolean started = false;
    // 子弹速度
    private int bulletSpeed = 5;
    // 发射间隔
    private int firePeriod = 2000;

    public Tank(){}

    public Tank(int x, int y,Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public int getFirePeriod() {
        return firePeriod;
    }

    public void setFirePeriod(int firePeriod) {
        this.firePeriod = firePeriod;
    }

    public int getLive() {
        return live;
    }

    public void setLive(int live) {
        this.live = live;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public Vector<Bullet> getBullets() {
        return bullets;
    }

    public void setBullets(Vector<Bullet> bullets) {
        this.bullets = bullets;
    }

    public boolean isRemoved() {
        return removed;
    }

    public void setRemoved(boolean removed) {
        this.removed = removed;
    }

    public boolean isStarted() {
        return started;
    }

    public void setStarted(boolean started) {
        this.started = started;
    }

    public int getBulletSpeed() {
        return bulletSpeed;
    }

    public void setBulletSpeed(int bulletSpeed) {
        this.bulletSpeed = bulletSpeed;
    }

    public int getTankWidth() {
        return tankWidth;
    }

    public void setTankWidth(int tankWidth) {
        this.tankWidth = tankWidth;
    }

    public int getTankHeight() {
        return tankHeight;
    }

    public void setTankHeight(int tankHeight) {
        this.tankHeight = tankHeight;
    }

    public int getWheelWidth() {
        return wheelWidth;
    }

    public void setWheelWidth(int wheelWidth) {
        this.wheelWidth = wheelWidth;
    }

    public int getBodyOffset() {
        return bodyOffset;
    }

    public void setBodyOffset(int bodyOffset) {
        this.bodyOffset = bodyOffset;
    }

    public int getBodyWidth() {
        return bodyWidth;
    }

    public void setBodyWidth(int bodyWidth) {
        this.bodyWidth = bodyWidth;
    }

    public int getBodyHeight() {
        return bodyHeight;
    }

    public void setBodyHeight(int bodyHeight) {
        this.bodyHeight = bodyHeight;
    }

    public int getPgWidth() {
        return pgWidth;
    }

    public void setPgWidth(int pgWidth) {
        this.pgWidth = pgWidth;
    }

    public int getPgHeight() {
        return pgHeight;
    }

    public void setPgHeight(int pgHeight) {
        this.pgHeight = pgHeight;
    }

    /**
     * 绘制爆炸效果
     * @param g
     */
    public void drawBoom(Graphics g){
        g.setColor(Color.YELLOW);
        g.fillOval(x,y,30,30);
        g.setColor(Color.GREEN);
        g.fillOval(x + 30,y + 10,40,40);
        g.setColor(Color.RED);
        g.fillOval(x ,y + 20,55,55);
        g.setColor(Color.BLUE);
        g.fillOval(x + 10,y + 20,45,45);
    }

    private int tankWidth = 50;
    private int tankHeight = 50;
    //履带宽度
    private int wheelWidth = 10;
    //车体后移
    private int bodyOffset = 5;
    //车体宽度
    private int bodyWidth = 30;
    //车体高度
    private int bodyHeight = 40;
    //炮管宽度
    private int pgWidth = 6;
    //炮管高度
    private int pgHeight = 10;
    /**
     * 绘制坦克
     * @param g
     */
    public void drawTank(Graphics g){
        if(this.removed){
            return;
        }
        if(this.live <= 0){
            drawBoom(g);
            return;
        }
        g.setColor(color);
        switch (direction){
            case DIRECTION_UP:
                //左履带
                g.fill3DRect(x,y,wheelWidth,tankHeight,false);
                //车体
                g.fill3DRect(x + wheelWidth,y + bodyOffset,bodyWidth,bodyHeight,false);
                //右履带
                g.fill3DRect(x + wheelWidth + bodyWidth, y, wheelWidth,tankHeight,false);
                //炮塔
                g.fillOval(x + wheelWidth,y + bodyOffset + (tankHeight - bodyHeight) / 2,bodyWidth,bodyWidth);
                //炮管
                g.fillRect(x + wheelWidth + bodyWidth / 2 - pgWidth / 2,y,pgWidth,pgHeight);
                break;
            case DIRECTION_DOWN:
                //左履带
                g.fill3DRect(x,y,wheelWidth,tankHeight,false);
                //车体
                g.fill3DRect(x + wheelWidth,y + bodyOffset,bodyWidth,bodyHeight,false);
                //右履带
                g.fill3DRect(x + wheelWidth + bodyWidth, y, wheelWidth,tankHeight,false);
                //炮塔
                g.fillOval(x + wheelWidth,y + bodyOffset + (tankHeight - bodyHeight) / 2,bodyWidth,bodyWidth);
                //炮管
                g.fillRect(x + wheelWidth + bodyWidth / 2 - pgWidth / 2,y + tankHeight - pgHeight + 1,pgWidth,pgHeight);
                break;
            case DIRECTION_LEFT:
                //右履带
                g.fill3DRect(x , y, tankHeight,wheelWidth,false);
                //车体
                g.fill3DRect(x + bodyOffset,y + wheelWidth,bodyHeight,bodyWidth,false);
                //左履带
                g.fill3DRect(x , y + wheelWidth + bodyWidth, tankHeight,wheelWidth,false);
                //炮塔
                g.fillOval(x + bodyOffset + (tankHeight - bodyHeight) / 2,y + wheelWidth,bodyWidth,bodyWidth);
                //炮管
                g.fillRect(x,y + wheelWidth + (bodyWidth - pgWidth) / 2,pgHeight,pgWidth);
                break;
            case DIRECTION_RIGHT:
                //右履带
                g.fill3DRect(x , y, tankHeight,wheelWidth,false);
                //车体
                g.fill3DRect(x + bodyOffset,y + wheelWidth,bodyHeight,bodyWidth,false);
                //左履带
                g.fill3DRect(x , y + wheelWidth + bodyWidth, tankHeight,wheelWidth,false);
                //炮塔
                g.fillOval(x + bodyOffset + (tankHeight - bodyHeight) / 2,y + wheelWidth,bodyWidth,bodyWidth);
                //炮管
                g.fillRect(x + tankHeight - pgHeight + 1,y + wheelWidth + (bodyWidth - pgWidth) / 2,pgHeight,pgWidth);
                break;
        }
        for(int i = 0;i < bullets.size();i++){
            Bullet bullet = bullets.get(i);
            //绘制子弹
            if(!bullet.isRemoved()) {
                g.fillOval(bullet.getX(), bullet.getY(), Bullet.WIDTH, Bullet.HEIGHT);
            }else {
                bullets.remove(i);
            }
        }
    }

    /**
     * 开炮
     */
    public void fire(){
        //死亡状态不能开炮
        if(this.live <= 0){
            return;
        }
        //创建子弹并设置坐标和方向
        Bullet bullet = null;
        switch(direction){
            case DIRECTION_UP:
                bullet = new Bullet(x + (tankWidth - pgWidth)/2,y,DIRECTION_UP);
                break;
            case DIRECTION_DOWN:
                bullet = new Bullet(x + (tankWidth - pgWidth)/2,y + tankHeight - Bullet.HEIGHT,DIRECTION_DOWN);
                break;
            case DIRECTION_LEFT:
                bullet = new Bullet(x,y + (tankWidth - pgWidth)/2,DIRECTION_LEFT);
                break;
            case DIRECTION_RIGHT:
                bullet = new Bullet(x + tankHeight - Bullet.HEIGHT,y + (tankWidth - pgWidth)/2,DIRECTION_RIGHT);
                break;
        }
        bullet.setSpeed(bulletSpeed);
        bullets.add(bullet);
        //执行子弹的线程控制移动
        MyGame.getInstance().threadPool.execute(bullet);
    }

    public void move(){
        if(this.live <= 0){
            return;
        }
        //向各方向移动
        switch (direction){
            case DIRECTION_UP:
                if(y >= 0)
                    y -= speed;
                break;
            case DIRECTION_DOWN:
                if(y <= PANEL_HEIGHT - tankHeight)
                    y += speed;
                break;
            case DIRECTION_LEFT:
                if(x >= 0)
                    x -= speed;
                break;
            case DIRECTION_RIGHT:
                if(x <= PANEL_WIDTH - tankWidth)
                    x += speed;
                break;
        }
    }

    /**
     * 判断是否能够移入前面的矩形位置
     * 防止坦克和坦克或障碍物重叠
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public boolean notInRect(int x, int y, int width, int height){
        //左边重叠
        if(this.x + this.tankWidth > x && this.x < x
                && this.y + this.tankHeight > y && this.y < y + height){
            return false;
        }
        //右边重叠
        if(this.x > x && this.x < x + width && this.y + this.tankHeight > y &&
                this.y < y + height){
            return false;
        }
        //上边重叠
        if(this.y + this.tankHeight > y && this.y < y &&
            this.x + this.tankWidth >x && this.x < x + width){
            return false;
        }
        //下边重叠
        if(this.y > y  && this.y < y + height &&
                this.x + this.tankWidth >x && this.x < x + width){
            return false;
        }
        return true;
    }
}
